"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_request_common = require("./common.js");
class Request {
  constructor(config = {}, reqInterceptor = null, resInterceptor = null, successHandler = null, failHandler = null, completeHandler = null) {
    this.baseUrl = config.baseUrl;
    this.header = config.header || {
      "Content-Type": "application/json;charset=UTF-8"
    }, // global callback for success/fail/complete. They are also response interceptors.
    this.success = successHandler;
    this.fail = failHandler;
    this.complete = completeHandler;
    this.requestInterceptor = reqInterceptor;
    this.responseInterceptor = resInterceptor;
  }
  // type: request/upload/download.
  // the success/fail/complete handler will not override the global, it will just call after global
  async request(options, successHandler = null, failHandler = null, completeHandler = null) {
    const task = options.task || false;
    const type = options.type || "request";
    let config = null;
    try {
      config = await utils_request_common.requestConfig(this, options, successHandler, failHandler, completeHandler);
    } catch (e) {
      return Promise.reject(e);
    }
    if (!config || typeof config != "object") {
      return Promise.reject({});
    }
    const that = this;
    if (task) {
      config["success"] = (response) => {
        if (that.responseInterceptor) {
          that.responseInterceptor(response, config);
        }
        that.success && that.success(response);
        successHandler && successHandler(response);
      };
      config["fail"] = (response) => {
        that.fail && that.fail(response);
        failHandler && failHandler(response);
      };
      config["complete"] = (response) => {
        that.complete && that.complete(response);
        completeHandler && completeHandler(response);
      };
      if (type === "request") {
        return common_vendor.index.request(config);
      } else if (type === "upload") {
        return common_vendor.index.uploadFile(config);
      } else {
        return common_vendor.index.downloadFile(config);
      }
    }
    return new Promise((resolve, reject) => {
      config["success"] = (response) => {
        let _res = null;
        if (that.responseInterceptor) {
          _res = that.responseInterceptor(response, config);
        }
        that.success && that.success(response);
        successHandler && successHandler(response);
        if (_res.wakaryReqToReject) {
          delete _res.wakaryReqToReject;
          reject(_res);
        } else {
          resolve(_res);
        }
      };
      config["fail"] = (error) => {
        that.fail && that.fail(error);
        failHandler && failHandler(error);
        reject(error);
      };
      config["complete"] = (response) => {
        that.complete && that.complete(response);
        completeHandler && completeHandler(response);
      };
      if (type === "request") {
        common_vendor.index.request(config);
      } else if (type === "upload") {
        common_vendor.index.uploadFile(config);
      } else {
        common_vendor.index.downloadFile(config);
      }
    });
  }
}
exports.Request = Request;
